package com.elaticsearch.doc;

import com.elaticsearch.test.Search;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

/**
 * @author:左泽林
 * @date:日期:2021-08-28-时间:16:35
 * @message:
 */
public class doc_query {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("172.20.17.43",9200,"http"))
        );

        /*try {
            //查询数据
            SearchRequest search = new SearchRequest();

            //1.查询索引中全部的数据
            search.indices("user");
            SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
            search.source(query);

            SearchResponse response = esClient.search(search, RequestOptions.DEFAULT);

            System.out.println("===============操作结果==============");
            System.out.println(response.getHits().getTotalHits());
            System.out.println(response.getTook());
            for (SearchHit hit : response.getHits()) {
                System.out.println(hit.getSourceAsString());
            }
            System.out.println("===============操作结果==============");
        }catch (Exception e){
            System.out.println(e);
        }finally {
            esClient.close();
        }*/


        /*try {
            //条件查询
            SearchRequest search = new SearchRequest();

            //1.查询索引中全部的数据
            search.indices("user");
            //该条件查询语句：表示想要把年龄等于30的取出来
            SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30));
            search.source(query);

            SearchResponse response = esClient.search(search, RequestOptions.DEFAULT);

            System.out.println("===============操作结果==============");
            System.out.println(response.getHits().getTotalHits());
            System.out.println(response.getTook());
            for (SearchHit hit : response.getHits()) {
                System.out.println(hit.getSourceAsString());
            }
            System.out.println("===============操作结果==============");
        }catch (Exception e){
            System.out.println(e);
        }finally {
            esClient.close();
        }*/

        /*try {
            //分页查询
            SearchRequest search = new SearchRequest();

            //1.查询索引中全部的数据
            search.indices("user");
            //该条件查询语句：表示想要把年龄等于30的取出来
            SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
            */

        /*
            builder.from(3);
            builder.size(4);

            search.source(builder);

            SearchResponse response = esClient.search(search, RequestOptions.DEFAULT);

            System.out.println("===============操作结果==============");
            System.out.println(response.getHits().getTotalHits());
            System.out.println(response.getTook());
            for (SearchHit hit : response.getHits()) {
                System.out.println(hit.getSourceAsString());
            }
            System.out.println("===============操作结果==============");
        }catch (Exception e){
            System.out.println(e);
        }finally {
            esClient.close();
        }*/


        try {
            //过滤字段
            SearchRequest search = new SearchRequest();

            //1.查询索引中全部的数据
            search.indices("user");
            //该条件查询语句：表示想要把年龄等于30的取出来
            SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
            /*过滤规则*/
            //选则你要排除的字段
            String[] excludes={"age"};
            //选择你要包含的字段
            String[] includes={};
            builder.fetchSource(includes,excludes);

            search.source(builder);

            SearchResponse response = esClient.search(search, RequestOptions.DEFAULT);

            System.out.println("===============操作结果==============");
            System.out.println(response.getHits().getTotalHits());
            System.out.println(response.getTook());
            for (SearchHit hit : response.getHits()) {
                System.out.println(hit.getSourceAsString());
            }
            System.out.println("===============操作结果==============");
        }catch (Exception e){
            System.out.println(e);
        }finally {
            esClient.close();
        }
    }
}
